﻿using System;
using System.Linq;
using System.Reflection;
using AutoMapper;

namespace API.Helpers
{
    public static class AutoMapperHelper
    {
        static AutoMapperHelper()
        {
            Mapper.Initialize(x => { });

        }


        //public static void AddProfiles(Assembly assembly)
        //{
        //    GetConfiguration(assembly, Mapper.Configuration);
        //}

        public static void RegisterProfiles(Assembly assembly)
        {
            GetConfiguration(assembly, Mapper.Configuration);
            //Mapper.Initialize(x => GetConfiguration(assembly, Mapper.Configuration));
        }

        private static void GetConfiguration(Assembly assembly, IConfiguration configuration)
        {
            var profiles = assembly.GetTypes().Where(x => typeof(Profile).IsAssignableFrom(x));
            foreach (var profile in profiles)
                configuration.AddProfile(Activator.CreateInstance(profile) as Profile);
        }

        public static Func<IMappingExpression<TSource, TDestination>> CreateMap<TSource, TDestination>(IMappingExpression<TSource, TDestination> mappingExpression)
        {
            return () => mappingExpression;
        }
    }
}
